package com.common.base.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.text.TextUtils;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentController;
import androidx.fragment.app.FragmentHostCallback;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.MyFragmentManager;

import com.common.base.utils.WeakHandler;

import java.lang.reflect.Field;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class CommonUtil {

    public static String getIpAddress(Context mContext) {
        String ip = getIpAddress1(mContext);
        if (TextUtils.isEmpty(ip)) {
            ip = getIpAddress2();
            if ("0.0.0.0".equals(ip)) {
                return null;
            }
        }
        return ip;
    }

    private static String getIpAddress1(Context mContext) {
        try {
            @SuppressLint("WifiManagerLeak") WifiManager wifiManager =
                    (WifiManager) mContext.getSystemService(
                            Context.WIFI_SERVICE);
            @SuppressLint("MissingPermission") WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ip = wifiInfo.getIpAddress();
            if (ip == 0) {
                return null;
            }
            return formatIpAddress(ip);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getIpAddress2() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private static String formatIpAddress(int ipAddress) {
        return (ipAddress & 0xFF) + "." +
                ((ipAddress >> 8) & 0xFF) + "." +
                ((ipAddress >> 16) & 0xFF) + "." +
                (ipAddress >> 24 & 0xFF);
    }

    public static void setHandler(Activity activity) {
        if (activity instanceof FragmentActivity == false) {
            return;
        }
        try {
            Field f = FragmentActivity.class.getDeclaredField("mFragments");
            f.setAccessible(true);
            FragmentController mFragments = (FragmentController) f.get(activity);

            f = FragmentController.class.getDeclaredField("mHost");
            f.setAccessible(true);
            FragmentHostCallback callback = (FragmentHostCallback) f.get(mFragments);
            f = FragmentHostCallback.class.getDeclaredField("mHandler");
            f.setAccessible(true);
            Handler h = new WeakHandler();
            f.set(callback, h);

            f = FragmentHostCallback.class.getDeclaredField("mFragmentManager");
            f.setAccessible(true);
            FragmentManager manager = new MyFragmentManager();
            f.set(callback, manager);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
